"""
图像的卷积
"""
from django.contrib.admin.utils import flatten
from scipy import signal # 卷积
# from scipy import misc # 读取图像
# from scipy import ndimage as sn # 读取图像
import numpy as np
import matplotlib.pyplot as plt
import imageio.v2 as imageio

# img = misc.imread("../data/zebra.png")
# img = sn.imread("../data/zebra.png")
# 读取灰度图像
img = imageio.imread("../data/zebra.png",pilmode='L')

print(img.shape)

# 垂直方向
flt0 = np.array([[-1,0,1],
                 [-2,0,2],
                 [-1,0,1]])

# 水平方向
flt1 = np.array([[1,2,1],
                 [0,0,0],
                 [-1,-2,-1]])

conv1 = signal.convolve2d(img,flt0,boundary='fill',mode='same').astype('int32')

conv2 = signal.convolve2d(img,flt1,boundary='fill',mode='same').astype('int32')

print(conv1)

plt.figure('Filter2D')
plt.subplot(1,3,1)
plt.imshow(img,cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(1,3,2)
plt.imshow(conv1,cmap='gray')
plt.xticks([])
plt.yticks([])

plt.subplot(1,3,3)
plt.imshow(conv2,cmap='gray')
plt.xticks([])
plt.yticks([])


plt.show()